from __future__ import absolute_import

from scopt.util import message as msg_util
from scopt.mpu import devel
from scopt.util.message_types import STRING
from . import message

_CONTEXT_HANDLERS = {}


def set_context_handlers(context_handlers):
    global _CONTEXT_HANDLERS
    _CONTEXT_HANDLERS = context_handlers


@message(type='devel.mpu_get',
         version='0.1',
         reqd={'message_type': STRING(required=True)},
         resd={})
def handle(data_packet, context):
    reqd = msg_util.get_request_data(data_packet)
    message_type = reqd['message_type']
    if message_type in _CONTEXT_HANDLERS:
        handler = _CONTEXT_HANDLERS[message_type]
        data_packet = devel.mpu_get(data_packet, handler)
        data_packet['status'] = 'FIN'
    return data_packet
